#coding=utf-8

'''
作业：一维谐振子变分法：
$$
\hat{H} = -\frac{d^2}{d x^2} + x^2
$$
- 猜测：波函数$\psi \propto e^{-\alpha x^2}$，优化 $\alpha$ 使得 $\langle \psi | \hat{H} | \psi \rangle$　达到极小值。

做尝试、分析结果、进行记录和讨论。
'''

import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate
from scipy.optimize import minimize

def f1(x,alpha): # < psi | H | psi >
    return ( 2*alpha + (1-4*alpha*alpha)*x*x )*np.exp(-2*alpha*x*x)
def f2(x,alpha): # < psi | psi >
    return np.exp(-2*alpha*x*x)
def f(alpha): # < psi | H | psi > / < psi | psi >
    return integrate.quad(f1, -np.inf, np.inf, alpha)[0]/integrate.quad(f2, -np.inf, np.inf, alpha)[0]

x = np.arange(0.1,2,0.1)
y = [ f(alpha) for alpha in x ]
plt.plot(x,y) # 画个图看看
plt.show()
res = minimize(f, 0.1, method='nelder-mead', options={'xatol':1e-8, 'disp': True} )
print("minimum is at x = ", res.x)

